// pages/index/home/home.js
var httpUtil = require('../../../utils/httpUtil.js')
var uiUtil = require('../../../utils/uiUtil.js')
var util = require('../../../utils/util.js')
const app = getApp()
var that
const REFRESH_TIME_GAP = 1000 * 60; //刷新时间间隔
Page({

  /**
   * 页面的初始数据
   */
  data: {
    identity:null,

    homeData: null,

    nextRefreshTime: 0, //下次可刷新时间


    showConfirmDialog: false, //是否显示
    confirmDialogCount: 1, //初始数量
    confirmDialogType: 1, //1:加入购物车 2：立即购买
    confirmDialogGood: {}, //确认框显示的商品信息
    dialogInputFocus: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    uiUtil.showLoading("正在加载中..")
    that.requestHomeData()
    that.setData({
      identity:app.globalData.identity
    })
  },
  onShow: function() {
    var curTime = new Date().getTime();
    if (curTime > this.data.nextRefreshTime + REFRESH_TIME_GAP * 4) {
      wx.startPullDownRefresh({})
      return;
    }
  },
  onPullDownRefresh: function() {
    var curTime = new Date().getTime();
    if (curTime <= this.data.nextRefreshTime) {
      wx.stopPullDownRefresh()
      return;
    } else {
      that.setData({
        nextRefreshTime: curTime + REFRESH_TIME_GAP
      })
      that.requestHomeData()
    }
  },
  onShareAppMessage: function(res) {
    return util.handleShareApp()
  },
  

  /**
   * 获取首页数据
   */
  requestHomeData() {
    httpUtil.PostRequest("home_data", {}, {
      success(res) {
        that.setData({
          homeData: res
        })
      },
      fail(code, res) {},
      complete() {
        uiUtil.hideLoading()
        wx.stopPullDownRefresh()
      }
    })
  },


  /**
   * 首页轮播图点击
   */
  homeSwiperClick(e) {
    var content = e.currentTarget.dataset.content
    util.handleHref(content)
  },

  /**
   * 农事提醒item点击
   */
  onEventItemClick(e) {
    var url = e.currentTarget.dataset.url
    if (url) {
      wx.navigateTo({
        url: '/pages/web/web?url=' + decodeURIComponent(url),
      })
    }
  },
  /**
     * 添加商品进入购物车
     */
  addCartClick(e) {
    var index = e.currentTarget.dataset.index
    var goodInfo = this.data.homeData.goodList[index]
    this.setData({
      confirmDialogGood: goodInfo,
      confirmDialogCount: 1
    })
    this.showConfirmDialog()

  },

  addCartClickConfirm() {
    var count = this.data.confirmDialogCount
    if (count <= 0) {
      uiUtil.showToast("请选择数量")
      return;
    }

    var goodId = this.data.confirmDialogGood.goodId
    var skuId = this.data.confirmDialogGood.skuId
    httpUtil.PostRequest("add_good_to_card", {
      goodId: goodId,
      skuId: skuId,
      buyCount: count
    }, {
        success(res) {
          that.hideConfirmDialog()
          uiUtil.showToast("添加购物车成功")
          app.cartSelectsAdd(goodId, skuId)
        },
        fail(code, res) {
          uiUtil.showToast(res || "添加购物车失败")
        },
        complete() { }
      })
  },

  /* 点击减号 */
  onConfirmDialogCut: function() {
    var confirmDialogCount = this.data.confirmDialogCount || 0;
    // 如果大于1时，才可以减  
    if (confirmDialogCount <= 1) { //原数量为0
      return;
    } else {
      var confirmDialogCount = confirmDialogCount - 1
      this.setData({
        confirmDialogCount: confirmDialogCount,
      });
    }
  },
  /* 点击加号 */
  onConfirmDialogAdd: function() {
    var confirmDialogCount = this.data.confirmDialogCount || 0;
    confirmDialogCount++;
    this.setData({
      confirmDialogCount: confirmDialogCount,
    });
  },

  onConfirmDialogInput(e) {
    var count = e.detail.value
    this.setData({
      confirmDialogCount: count
    })
  },

  //显示对话框
  showConfirmDialog: function () {
    that = this;
    that.setData({
      showConfirmDialog: true
    })
  },
  //隐藏对话框
  hideConfirmDialog: function () {
    that = this;
    that.setData({
      showConfirmDialog: false,
      dialogInputFocus: false
    })
  },

  onConfirmDialogInputClick() {
    this.setData({
      dialogInputFocus: true,
    })
  },
  onConfirmDialogBtnClick() {
    if (this.data.confirmDialogType == 1) { //加入购物车
      that.addCartClickConfirm()
    } else {

    }

  },

})